package com.lanou3g.mynewstest.module.videos;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.beans.VideoBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.lanou3g.mynewstest.utils.TimeUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Created by dllo on 16/1/14.
 */
public class VideoAdapter extends BaseAdapter {
    private VideoBeans data;
    private Context context;

    public VideoAdapter(VideoBeans data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.getVideoList().size();
    }

    @Override
    public Object getItem(int position) {
        return data.getVideoList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyVideoViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_video, parent, false);
            holder = new MyVideoViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyVideoViewHolder) convertView.getTag();
        }
        holder.title.setText(data.getVideoList().get(position).getTitle());
        holder.playCount.setText("" + data.getVideoList().get(position).getPlayCount());
        holder.description.setText(data.getVideoList().get(position).getDescription());
        holder.time.setText(""+data.getVideoList().get(position).getLength());

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VideoActivity.goToVideoActivity(context,data.getVideoList().get(position).getMp4_url());
            }
        });
        MyRequestQueue.setNetImage(holder.imageView, data.getVideoList().get(position).getCover());
        return convertView;
    }


    class MyVideoViewHolder {
        private NetworkImageView imageView;
        private TextView title, description, time, playCount;

        public MyVideoViewHolder(View view) {
            imageView = (NetworkImageView) view.findViewById(R.id.item_video_iv);
            title = (TextView) view.findViewById(R.id.item_video_title);
            description = (TextView) view.findViewById(R.id.item_video_description);
            time = (TextView) view.findViewById(R.id.item_video_time_tv);
            playCount = (TextView) view.findViewById(R.id.item_video_count_tv);
        }
    }
}
